
# -*- coding: utf-8 -*-
# @Author: Gemini by Tim Liu 
# @Date: 2024-07-06
# @Last Modified by: Tim Liu
# @Last Modified time: 2024-07-06

# @Description: Helper class for downloading remote files.

import requests
from core.exception import CustomException
from utils import status

class FileUtil:
    """
    Utility class for downloading remote files.
    """

    @staticmethod
    def download_file(url):
        """
        Downloads a remote file and returns the content as bytes.

        Args:
            url (str): The URL of the remote file.

        Returns:
            bytes: The downloaded data, or None if there's an error.
                    
        # Example usage
        url = "https://cdn.crewplus.ai/data/audio/2024/07/05/35896_yowc_3176.wav"  # Replace with your URL
        downloaded_data = FileUtil.download_file(url)

        if downloaded_data:
            print("File downloaded successfully!")
            # Process the downloaded data here (e.g., write to a file, use in transcription)
        else:
            print("Error downloading file.")            
        """

        try:
            response = requests.get(url, stream=True)
            response.raise_for_status()  # Raise exception for non-200 status codes

            return response.content
        except requests.exceptions.RequestException as e:
            raise CustomException("apiKey invalid", code=status.HTTP_ERROR)